package com.book.util;

import cn.hutool.core.io.FileUtil;
import org.mybatis.generator.api.MyBatisGenerator;
import org.mybatis.generator.config.Configuration;
import org.mybatis.generator.config.xml.ConfigurationParser;
import org.mybatis.generator.exception.InvalidConfigurationException;
import org.mybatis.generator.exception.XMLParserException;
import org.mybatis.generator.internal.DefaultShellCallback;

import java.io.File;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author SuperYu
 * 2024/4/7
 */
public class Generator {

    // 备份
    public static void backUp() {
        String mapperLocation = "src/main/java/com/woniu/mapper";
        File mapperFolder = new File(mapperLocation);

        File[] files = mapperFolder.listFiles();
        if (files != null && files.length != 0) {
            // 目录里面有文件，需要备份
            File backUpFolder = new File("src/main/java/com/woniu/backup");

            FileUtil.copyContent(mapperFolder, backUpFolder, false);
            System.out.println("--- back ok ----");
        }
        // 目录为空，就不用备份
    }


    // 逆向工程生成我们的代码：
    public static void create() {
        List<String> warnings = new ArrayList<>();
        boolean overwrite = true;
        ConfigurationParser cp = new ConfigurationParser(warnings);
        try {
            Configuration config = cp.parseConfiguration(
                    // Generator.class.getResourceAsStream("generatorConfig.xml"));
                    Generator.class.getClassLoader().getResourceAsStream("generatorConfig.xml"));

            DefaultShellCallback callback = new DefaultShellCallback(overwrite);
            MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
            myBatisGenerator.generate(null);

        } catch (XMLParserException e) {
            e.printStackTrace();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (InvalidConfigurationException e) {
            e.printStackTrace();
        }
    }

    // 再把备份的代码恢复
    public static void restore() {
        String backupLocation = "src/main/java/com/woniu/backup";
        File backUpFolder = new File(backupLocation);
        // 得到备份目录下的所有内容
        File[] files = backUpFolder.listFiles();
        if(files != null && files.length != 0){
            // 还原数据
            File mapperFolder = new File("src/main/java/com/woniu/mapper");
            // 使用工具进行复制, 注意要覆盖写入
            FileUtil.copyContent(backUpFolder,mapperFolder,true);
            // 备份目录记得要删除
            boolean f = FileUtil.del(backUpFolder);
            System.out.println("备份发文件是否删除："+ f );
        }
    }


    public static void main(String[] args) {
        // 1：先备份
        backUp();
        // 2：再逆向工程生成代码。
        create();
        // 3: 原有数据，覆盖新的数据，backup再删除掉
        restore();
        System.out.println("--------ok----------");
    }


}
